package com.baselibrary.libs.Utils.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.baselibrary.libs.View.dialog.CustomDialog;

/**
 * Created by 35876 于萌萌
 * 创建日期: 14:57 . 2017年02月02日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class DialogUtils {

    static DialogUtils sDialogUtils;
    Activity activity;
    public DialogUtils(Activity activity) {
        this.activity =activity;
    }
    //单例模式
    public static DialogUtils  createDialogUtils(Activity activity){

        if (null==sDialogUtils){
            return sDialogUtils = new DialogUtils(activity);
        }
        return sDialogUtils;
    }

    int type= -1;
    String titleName;
    String message;
    int position = -1;
    String confirm;
    String cancel;
    boolean cancelable;

    public DialogUtils setType(int type){
        this.type =type;
        return this;
    }
    public DialogUtils setTitle(String titleName){
        this.titleName =titleName;
        return this;
    }
    public DialogUtils setMessage(String message){
        this.message =message;
        return this;
    }
    public DialogUtils setPosition(int position){
        this.position =position;
        return this;
    }
    public DialogUtils setConfirm(String confirm){
        this.confirm =confirm;
        return this;
    }
    public DialogUtils setCancelable(boolean cancelable){
        this.cancelable =cancelable;
        return this;
    }

    public DialogUtils setCancel(String cancel){
        this.cancel =cancel;
        return this;
    }
    public void show(int style,final DialogCallBack callBack){
        switch (style){
            case 0:

                AlertDialog.Builder builder0 = new AlertDialog.Builder(activity);
                style(builder0,callBack);
            break;
            case 1:
                CustomDialog.Builder builder1 = new CustomDialog.Builder(activity);
                style(builder1,callBack);
            break;
        }



       //



    }

    private void style( AlertDialog.Builder builder, final DialogCallBack callBack) {
        if (!TextUtils.isEmpty(titleName)){
            builder.setTitle(titleName); //设置标题
        }else{
            builder.setTitle("默认标题"); //设置默认
        }

        if (!TextUtils.isEmpty(message)){
            builder.setMessage(message); //设置内容
        }else{
            builder.setMessage("默认内容"); //设置内容
        }
        String confirmString = "";
        if (!TextUtils.isEmpty(confirm)){
            confirmString = confirm;
        }else{
            confirmString = "确定";
        }

        builder.setPositiveButton(confirmString, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //点的确定
                callBack.confirm(type,position);
            }
        });


        String cancelString = "";
        if (!TextUtils.isEmpty(cancel)){
            cancelString = cancel;
            builder.setNegativeButton(cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            callBack.cancel();
                            dialog.dismiss();
                        }
                    });
        }


        //  builder.create().show();
        // CustomDialog alertDialog = builder.create();
        AlertDialog alertDialog = builder.create();

        //设置外面是否能点击关闭
        if (cancelable){
            alertDialog.setCancelable(true);
        }else{
            alertDialog.setCancelable(false);
        }
        alertDialog.show();
        WindowManager.LayoutParams  lp= alertDialog.getWindow().getAttributes();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int displayWidth = displayMetrics.widthPixels;
        int displayHeight = displayMetrics.heightPixels;

        //定义宽度
        lp.width=(int) (displayWidth* 0.75f + 0.5f);
        //定义高度
        //lp.height=(int) (displayHeight* 0.50f + 0.5f);
        alertDialog.getWindow().setAttributes(lp);


    }

    private void style(CustomDialog.Builder builder, final DialogCallBack callBack) {
        if (!TextUtils.isEmpty(titleName)){
            builder.setTitle(titleName); //设置标题
        }else{
            builder.setTitle("默认标题"); //设置默认
        }

        if (!TextUtils.isEmpty(message)){
            builder.setMessage(message); //设置内容
        }else{
            builder.setMessage("默认内容"); //设置内容
        }
        String confirmString = "";
        if (!TextUtils.isEmpty(confirm)){
            confirmString = confirm;
        }else{
            confirmString = "确定";
        }

        builder.setPositiveButton(confirmString, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //点的确定
                callBack.confirm(type,position);
            }
        });


        String cancelString = "";
        if (!TextUtils.isEmpty(cancel)){
            cancelString = cancel;
            builder.setNegativeButton(cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            callBack.cancel();
                            dialog.dismiss();
                        }
                    });
        }


        //  builder.create().show();
        // CustomDialog alertDialog = builder.create();
        CustomDialog alertDialog = builder.create();

        //设置外面是否能点击关闭
        if (cancelable){
            alertDialog.setCancelable(true);
        }else{
            alertDialog.setCancelable(false);
        }
        alertDialog.show();
        WindowManager.LayoutParams  lp= alertDialog.getWindow().getAttributes();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int displayWidth = displayMetrics.widthPixels;
        int displayHeight = displayMetrics.heightPixels;

        //定义宽度
        lp.width=(int) (displayWidth* 0.75f + 0.5f);
        //定义高度
        //lp.height=(int) (displayHeight* 0.50f + 0.5f);
        alertDialog.getWindow().setAttributes(lp);
    }
}
